package com.easpeed.comon.bean

import com.baidu.mapapi.model.LatLng

open class LatLngCommon {


    @kotlin.jvm.JvmField
    var lat: Double = 0.0

    @kotlin.jvm.JvmField
    var lng: Double = 0.0

    @kotlin.jvm.JvmField
    var latitude: Double = 0.0

    @kotlin.jvm.JvmField
    var longitude: Double = 0.0

    constructor(lat: Double?, lng: Double?) {
        lat?.let {
            this.lat = it
            this.latitude = it
        }
        lng?.let {
            this.lng = it
            this.longitude = it
        }
    }

    constructor(latLng: Pair<Double, Double>?) {
        latLng?.let {
            this.lat = it.first
            this.latitude = it.first

            this.lng = it.second
            this.longitude = it.second
        }
    }

    /**
     * 逗号分隔
     */
    constructor(latLng: String?) {
        try {
            latLng?.let {
                val split = it.split(",")
                if (split.count() == 2) {
                    this.lat = split[0].toDouble()
                    this.latitude = split[0].toDouble()
                    this.lng = split[1].toDouble()
                    this.longitude = split[1].toDouble()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    constructor(lat: String?, lng: String?) {
        try {
            lat?.let {
                this.lat = it.toDouble()
                this.latitude = it.toDouble()
            }
            lng?.let {
                this.lng = it.toDouble()
                this.longitude = it.toDouble()
            }
        } catch (e: Exception) {
        }
    }

    public fun isNullLocation(): Boolean {
        if (lat == 0.0 || lng == 0.0 || lat == 4.9E-324 || lng == 4.9E-324) {
            return true
        }
        return false
    }

    fun getSave(): String {
        return "$lat,$lng"
    }

    override fun toString(): String {
        return "LatLngCommon(lat=$lat,$lng)"
    }


}
fun LatLngCommon?.toLatLng(): LatLng? {
    if (this == null) {
        return null
    }
    return LatLng(lat, lng)
}

/**
 * 位置是否相同
 */
fun LatLngCommon?.isSame(to: LatLngCommon?): Boolean {
    if (this == null || to == null) {
        return false
    }
    if (this.lat == to.lat && this.lng == to.lng) {
        return true
    }
    return false
}